package com.bjypc.youth.service.chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class FruitService {

	public static JFreeChart createBarChart() {
		CategoryDataset dataset = getDataSet2();
		JFreeChart chart = ChartFactory.createBarChart3D("水果产量图ˮ���ͼ", // ͼ图表标题
				"ˮ��水果", // 目录轴的显示标签
				"产量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // ͼ图标方向：水平、垂直
				true, // 是否显示图例
				true, // 是否生成工具
				true // 是否生成URL链接
				);
		return chart;
	}

	private static CategoryDataset getDataSet2() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "北京", "苹果");
		dataset.addValue(500, "北京", "荔枝");
		dataset.addValue(400, "北京", "香蕉");
		dataset.addValue(200, "北京", "梨子");
		dataset.addValue(300, "北京", "葡萄");
		dataset.addValue(500, "上海", "葡萄");
		dataset.addValue(600, "上海", "梨子");
		dataset.addValue(400, "上海", "香蕉");
		dataset.addValue(700, "上海", "苹果");
		dataset.addValue(300, "上海", "荔枝");
		dataset.addValue(300, "广州", "苹果");
		dataset.addValue(200, "广州", "梨子");
		dataset.addValue(500, "广州", "香蕉");
		dataset.addValue(400, "广州", "葡萄");
		dataset.addValue(700, "广州", "荔枝");
		return dataset;
	}

	public static JFreeChart createPaiChart() {
		DefaultPieDataset data = getDataSet();
		JFreeChart chart = ChartFactory.createPieChart3D("ˮ���ͼ水果产量图", // 图表标题
				data, // 数据集
				true, // 是否显示图例
				false, false);
		PiePlot plot = (PiePlot) chart.getPlot();
		resetPiePlot(plot);
		return chart;
	}

	private static void resetPiePlot(PiePlot plot) {
		String unitSytle = "{0}={1}({2})";

		plot.setNoDataMessage("暂时没有数据");
		plot.setNoDataMessagePaint(Color.red);

		// ָ�� section ��*�ߵĺ��(OutlinePaint����Ϊnull)
		plot.setOutlineStroke(new BasicStroke(0));

		// ���õ�һ�� section �Ŀ�ʼλ�ã�Ĭ����12���ӷ���
		plot.setStartAngle(90);

		plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,
				NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

		// ָ��ͼƬ��͸���
		plot.setForegroundAlpha(0.65f);

		// ����ǩ��ʾ��ʽ
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
				NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

		// ͼ����ʾ��ʽ
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
				unitSytle, NumberFormat.getNumberInstance(), new DecimalFormat(
						"0.00%")));
	}

	/**
	 * 获取一个饼状图的简单数据集对象
	 * 
	 * @return
	 */
	private static DefaultPieDataset getDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("苹果", 100);
		dataset.setValue("梨子", 200);
		dataset.setValue("葡萄", 300);
		dataset.setValue("香蕉", 400);
		dataset.setValue("荔枝", 500);
		return dataset;
	}
}
